//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.yaukie.base.aspect;

import java.lang.reflect.Method;
import java.util.Objects;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.yaukie.base.annotation.SetDataSource;
import org.yaukie.base.datasource.DataSourceRouterHolder;

@Component
@Aspect
@Order(1)
public class DataSourceAop {
    private static final Logger log = LoggerFactory.getLogger(DataSourceAop.class);

    public DataSourceAop() {
    }

    @Around("org.yaukie.base.arch.Architecture.dataSourcePointCut()")
    public Object doSetDataSource(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
        Method targetMethod = methodSignature.getMethod();
        SetDataSource setDataSource = (SetDataSource)AnnotationUtils.findAnnotation(targetMethod, SetDataSource.class);
        String routerKey = "";
        if (Objects.isNull(setDataSource)) {
            setDataSource = (SetDataSource)AnnotationUtils.findAnnotation(methodSignature.getDeclaringType(), SetDataSource.class);
        }

        routerKey = setDataSource.value().name();
        DataSourceRouterHolder.setRouterKey(routerKey);

        try {
            Object proceed = joinPoint.proceed();
            Object var7 = proceed;
            return var7;
        } catch (Exception var11) {
            log.error("DataSourceAop==>切换数据源出现异常{}", var11);
        } finally {
            DataSourceRouterHolder.removeRouterKey();
        }

        return null;
    }
}
